/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.digwener.entities.users;

/**
 *
 * @author Anton
 */

import com.digwener.entities.EntityHolder;
import com.digwener.entities.EntityException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import javax.xml.bind.*;

public class UserHolder implements EntityHolder{
    private static UserHolder instance = null;
    
   private Set<String> selected = new HashSet<String>();

   private String fileName = "default.xml";

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
     
    private UserHolder(){}
    
    
    public static UserHolder getInstance(){
        if(instance == null){
            instance = new UserHolder();
        }        
        return instance;
    }
    
     UserType userElem;

    public UserType getUserElem(boolean selectedOnly) {
        ObjectFactory factory = new ObjectFactory();
        userElem = factory.createUserType();
        if(!selectedOnly){
            userElem.setResponsibilites(respList);
        }else{
            List<ResponsibilityType> rtList = new ArrayList<ResponsibilityType>();
            for(ResponsibilityType rt:respList){
                if(selected.contains(rt.getRespName())){
                    rtList.add(rt);
                }
            }
            userElem.setResponsibilites(rtList);
        }
        
        userElem.setName(userName);
        return userElem;
    }
    
    private String userName;
    
    public String getUserName(){
        return userName;
    }
    
    public void setUserName(String userName){
        this.userName = userName;
    }
            
    private List<ResponsibilityType> respList;
    
    private void restoreSelected(){
        if(respList == null)
            return;
        for(ResponsibilityType rt:respList){
            if( selected.contains(rt.getRespName())){
                rt.setSelected(true);
            }
        }
    }
    
    
    public List<ResponsibilityType> getResps(){
        return respList;
    }
    
    public void loadFromXML(){
        JAXBElement<UserType> jaxbConnections = null;
        try{
           JAXBContext jc = JAXBContext.newInstance("com.digwener.entities.users"); 
           Unmarshaller m = jc.createUnmarshaller();	
           File f = new File(fileName);
           jaxbConnections =(JAXBElement<UserType>) m.unmarshal(f); 
        }catch(JAXBException je)
		{
			String message = je.getMessage();
                        
			System.out.println(message + " "+ je.toString());
                        return;
		}
        
        
        userElem = jaxbConnections.getValue();
        respList = userElem.getResponsibilities();
        userName = userElem.getName();
        restoreSelected();
    }
    
    private ResponsibilityType getByName(String respName){
        
        for(ResponsibilityType rt: respList){
            if(rt.getRespName().equals(respName)){
                return rt;
            }
        }
        return null;
    }
    
    public void select(String respName){
        selected.add(respName);
    }
    
    
    public void deselect(String respName){
        selected.remove(respName);
    }
    
    public void setSelected(String respName, boolean value){
        if(value){
            select(respName);
        }else{
            deselect(respName);
        }
    }
    
    public boolean isSelected(String respName){
        return selected.contains(respName);
    }
    
    public void selectAll(){
        for(ResponsibilityType rt:respList){
            selected.add(rt.respName);
        }
    }
    
    public void deselectAll(){
        selected.clear();
    }
    
    public void deleteSelected(){
        
        Set<ResponsibilityType> toRemove = new HashSet<ResponsibilityType>();
        for(ResponsibilityType rt:respList){
            if(selected.contains(rt.getRespName())){
                toRemove.add(rt);
            }
        }
        for(ResponsibilityType rt:toRemove){
            respList.remove(rt);
        }
        
    }
    
    @Override
    public void saveToXML(){
        ObjectFactory factory = new ObjectFactory();
        userElem = factory.createUserType();
        userElem.setResponsibilites(respList);
        userElem.setName(userName);
        JAXBElement<UserType> jaxbConnections = factory.createUsers(userElem);
        try{
            JAXBContext jc = JAXBContext.newInstance("com.digwener.entities.users"); 
            Marshaller m = jc.createMarshaller();	
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            File f = new File(fileName);
            m.marshal(jaxbConnections, f);
        }catch(JAXBException je)
        {
                String message = je.getMessage();
                System.out.println(message);
        }
    }
    
    ResponsibilityType getConnectorByName(String name) throws EntityException{
        if (respList == null)
            throw new EntityException("Connection Holder is not initialized");
        
        ResponsibilityType result = null;
        
        for(ResponsibilityType resp:respList){
            if(resp.getRespName().equals(name))
                return resp;
        }
        
        throw new EntityException("Specified responsibility does not exist");
    }
    
    public void merge(List<ResponsibilityType> resps){
        if (this.respList == null) {
            this.respList = new ArrayList<ResponsibilityType> ();
        }
        for(ResponsibilityType r:resps){
            boolean found = false;
            for(ResponsibilityType ri:this.respList){
                found = ri.getRespName().equals(r.getRespName());
                if (found) break;
            }
            
            if(!found){
                this.respList.add(r);
            }
        }
        restoreSelected();
        saveToXML();
    }

}
